package org.jim.server.processor.chat;

import org.jim.core.ImChannelContext;
import org.jim.core.config.ImConfig;
import org.jim.core.helper.MessageHelper;
import org.jim.core.packets.Message;
import org.jim.core.packets.pub.consts.ChatType;
import org.jim.core.packets.pub.pojo.JimChatBody;
import org.jim.server.config.ImServerConfig;
import org.jim.server.processor.SingleProtocolCmdProcessor;
import org.jim.server.util.ChatKit;
import org.jim.server.util.ImChatKit;

import java.util.List;

/**
 * @author WChao
 * @date 2018年4月3日 下午1:13:32
 */
public  class ImAsyncChatMessageProcessor implements SingleProtocolCmdProcessor {

    @Override
    public void process(ImChannelContext imChannelContext, Message message) {
        JimChatBody chatBody = (JimChatBody) message;
        ImServerConfig imServerConfig = ImConfig.Global.get();
        //开启持久化
        boolean isStore = ImServerConfig.ON.equals(imServerConfig.getIsStore());
        if (isStore) {
            //存储群聊消息;
            if (ChatType.CHAT_TYPE_PUBLIC.getNumber() == chatBody.getChatType()) {
                pushGroupMessages(imServerConfig, PUSH, STORE, chatBody, isStore);
            } else {
                //私聊+客服聊天
                String from = chatBody.getFrom();
                String to = chatBody.getTo();
                // ChatKit.sessionId(from, to); 以前的获取方式
                String sessionId = ImChatKit.sessionId(from, to);
                writeMessage(imServerConfig, STORE, USER + ":" + sessionId, chatBody);
                boolean isOnline = ChatKit.isOnline(to, isStore);
                if (!isOnline) {
                    writeMessage(imServerConfig, PUSH, USER + ":" + to + ":" + from, chatBody);
                }
            }
        }
        //doProcess(chatBody, imChannelContext);
    }


    /**
     * 推送持久化群组消息
     *
     * @param pushTable
     * @param storeTable
     * @param chatBody
     */
    private void pushGroupMessages(ImServerConfig imServerConfig, String pushTable, String storeTable, JimChatBody chatBody, boolean isStore) {
        MessageHelper messageHelper = imServerConfig.getMessageHelper();
        String group_id = chatBody.getGroupId();
        //先将群消息持久化到存储Timeline;
        writeMessage(imServerConfig, storeTable, GROUP + ":" + group_id, chatBody);
        List<String> userIds = messageHelper.getGroupHelper().getGroupUsers(group_id);
        //通过写扩散模式将群消息同步到所有的群成员
        for (String userId : userIds) {
            boolean isOnline = false;
            if (isStore && ImServerConfig.ON.equals(imServerConfig.getIsCluster())) {
                isOnline = messageHelper.getUserHelper().isOnline(userId);
            } else {
                isOnline = ChatKit.isOnline(userId, isStore);
            }
            if (!isOnline) {
                writeMessage(imServerConfig, pushTable, GROUP + ":" + group_id + ":" + userId, chatBody);
            }
        }
    }

    private void writeMessage(ImServerConfig imServerConfig, String timelineTable, String timelineId, JimChatBody chatBody) {
        MessageHelper messageHelper = imServerConfig.getMessageHelper();
        //messageHelper.getChatHelper().writeCacheMessage(timelineTable, timelineId, chatBody);
    }
}
